<template>
  <div class="app-container">


    <div class="app-table">
      <div class="addButton"><el-button type="primary" @click="openAddUpdDialog(null)">新增角色</el-button></div>
      <el-table class="my-custom-table" v-loading="listLoading" :data="list" element-loading-text="Loading" he border
        fit highlight-current-row>
        <el-table-column align="center" label="序号" width="100%">
          <template slot-scope="scope">
            {{ scope.$index + 1 }}
          </template>
        </el-table-column>
        <el-table-column label="系统名称" align="center">
          <template slot-scope="scope">
            {{ scope.row.systemSystemName }}
          </template>
        </el-table-column>
        <el-table-column label="角色编号" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.roleNum }}</span>
          </template>
        </el-table-column>
        <el-table-column label="角色名称" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.roleName }}</span>
          </template>
        </el-table-column>
        <el-table-column label="角色级别" align="center">
          <template slot-scope="scope">
            {{ scope.row.level }}
          </template>
        </el-table-column>
        <el-table-column label="角色描述" align="center">
          <template slot-scope="scope">
            {{ scope.row.remarks }}
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="120">
          <template slot-scope="scope">
            <el-button @click="openAddUpdDialog(scope.row)" type="text" size="small">
              编辑
            </el-button>
            <el-button @click="openDelDialog(scope.row.id)" type="text" size="small">
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination v-show="total > 0" :total="total" :page.sync="listQuery.pageIndex" :limit.sync="listQuery.pageSize"
        @pagination="getList" />
    </div>
    <el-dialog title="提示" :visible.sync="dialogVisible" width="30%" :before-close="handleClose">
      <span>这是一段信息</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="deleteRow()">确 定</el-button>
      </span>
    </el-dialog>
    <el-dialog :title="title" :visible.sync="addOrUpdateDialog" center>
      <el-form :model="roleObject" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">

        <el-form-item label="编号" v-show="false">
          <el-input v-model="roleObject.id"></el-input>
        </el-form-item>
        <el-form-item label="系统名称" prop="systemId">
          <el-select v-model="roleObject.systemId" placeholder="请选择系统">
            <el-option label="测试系统" value="4326811932741861377"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="角色编号" prop="roleNum">
          <el-input v-model="roleObject.roleNum"></el-input>
        </el-form-item>
        <el-form-item label="角色名称" prop="roleName">
          <el-input v-model="roleObject.roleName"></el-input>
        </el-form-item>
        <el-form-item label="角色级别" prop="level">
          <el-input v-model="roleObject.level"></el-input>
        </el-form-item>
        <el-form-item label="角色描述" prop="remarks">
          <el-input v-model="roleObject.remarks"></el-input>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="canAddUpdDialog()">取 消</el-button>
        <el-button type="primary" @click="submitRoleInfo('ruleForm')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { queryRolePages, deleteRoleById, updateRoleInfo, createRoleInfo } from '@/api/role'
import Pagination from '@/components/Pagination'
import { title } from '@/settings';
import { Message } from 'element-ui'
export default {
  name: 'Dashboard',
  components: { Pagination },
  data() {
    return {
      list: null,
      listLoading: true,
      listQuery: {
        pageIndex: 1,
        pageSize: 10
      },
      total: 0,
      dialogVisible: false,
      addOrUpdateDialog: false,
      deleteId: null,
      roleObject: {

      },
      rules: {
        systemId: [
          { required: true, message: '请选择系统', trigger: 'blur' },
        ],
        roleNum: [
          { required: true, message: '请输入角色标识', trigger: 'change' }
        ],
        roleName: [
          { required: true, message: '请输入角色名称', trigger: 'change' }
        ],
        level: [
          { required: true, message: '请输入角色级别', trigger: 'change' }
        ]
      },
      title: ""
    }
  },
  created() {
    this.getList()
  },
  methods: {
    canAddUpdDialog() {
      this.addOrUpdateDialog = false

    },
    openDelDialog(id) {
      this.deleteId = id;
      this.dialogVisible = true;
    },
    openAddUpdDialog(row) {
      if (row) {
        this.title = "修改角色信息"
        //深拷贝一下
        this.roleObject = JSON.parse(JSON.stringify(row))
      } else {
        this.roleObject = {
        }
        this.title = "新增角色信息"
      }

      this.addOrUpdateDialog = true
    },
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          done();
        })
        .catch(_ => { });
    },
    async getList() {
      this.listLoading = true
      const { data } = await queryRolePages(this.listQuery).finally({
      })
      this.list = data.data
      this.total = data.totalCount
      this.listLoading = false
    },
    async deleteRow() {
      this.listLoading = true
      await deleteRoleById({ Id: this.deleteId }).then(res => {
        this.listLoading = false
        this.dialogVisible = !res;
        if (res) {
          Message({
            message: "删除成功",
            type: 'info',
            duration: 5 * 1000
          })
          this.getList();
        }
      }).catch(err => {
        this.listLoading = false
        this.dialogVisible = false;
      })

    },
    async submitRoleInfo(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          if (this.roleObject.id) {
            await updateRoleInfo(this.roleObject).then(res => {
              this.listLoading = false
              this.addOrUpdateDialog = !res;
              if (res) {
                Message({
                  message: "修改成功",
                  type: 'info',
                  duration: 5 * 1000
                })
                this.getList();
              }
            }).catch(err => {
              this.listLoading = false
              this.addOrUpdateDialog = false;
            })
          } else {
            await createRoleInfo(this.roleObject).then(res => {
              this.listLoading = false
              this.addOrUpdateDialog = !res;
              if (res) {
                Message({
                  message: "新增成功",
                  type: 'info',
                  duration: 5 * 1000
                })
                this.getList();
              }
            }).catch(err => {
              this.listLoading = false
              this.addOrUpdateDialog = false;
            })
          }
        }
      });

    }
  }
}
</script>

<style lang="scss" scoped>
.app-container {
  .app-table {
    margin: 25px 100px 75px 100px
  }

  .addButton {
    margin: 0px 0px 20px 94%
  }

}

/* 只修改特定表格的表头颜色 */
.my-custom-table .el-table th {
  background-color: #F56C6C;
  /* 你想要的颜色 */
}
</style>
